<?php

namespace App\Models\Enroll;

use App\Contracts\Repositories\EnrollRepository;
use Ramsey\Uuid\Uuid;

class EnrollService
{
    //
    public $enrollRepository;

    /**
     * PrizePaymentService constructor.
     * @param $enrollRepository
     */
    public function __construct(EnrollRepository $enrollRepository)
    {
        $this->enrollRepository = $enrollRepository;
    }

    public function insert($prizePayment)
    {
        return $this->enrollRepository->insert($prizePayment);
    }

    public function create($prizePayment)
    {
        return $this->enrollRepository->create($prizePayment);
    }

    public function modify($prizePayment, $conditions)
    {
        return $this->enrollRepository->modify($prizePayment, $conditions);
    }

    public function update($fields, $conditions)
    {
        return $this->enrollRepository->update($fields, $conditions);
    }

    public function delete($conditions)
    {
        return $this->enrollRepository->delete($conditions);
    }

    public function find($conditions)
    {
        return $this->enrollRepository->find($conditions);
    }

    public function findAll($conditions)
    {
        return $this->enrollRepository->findAll($conditions);
    }

    public function getEnrollServicePageList($request)
    {
        $start = (int)$request->query->get('offset', 0);
        $limit = (int)$request->query->get('limit', 10);
        $keyword = $request->query->get('search');
        $sort = $request->query->get('sort', 'created_at');
        $order = $request->query->get('order', 'desc');
        $sortArray = [[$sort, $order]];
        $activities_id = $request->query->get('activities_id');
        if (!Uuid::isValid($activities_id)) {
            return ['total' => 0, 'rows' => []];
        }
        $conditions = [];

        if (!is_null($keyword)) {
            $conditions[] = ['order_name', 'like', '%' . $keyword . '%'];
        }
        if (!empty($activities_id)) {
            $conditions[] = ['activities_id',$activities_id];
        }
        $list = $this->enrollRepository->findByConditionsPage($conditions, $sortArray, $limit, $start);
        $list = $list->toArray();
        $total = $this->enrollRepository->countByConditions($conditions);
        return ['total' => $total, 'rows' => $list];
    }
}
